package com.zhuss.product.processor;

import com.zhuss.bean.bo.product.line.BindLineBo;
import com.zhuss.bean.bo.product.line.CreateLineSiteBo;
import com.zhuss.bean.vo.product.line.DetailLineVo;
import com.zhuss.common.AbstractProcessor;
import com.zhuss.common.Processor;
import com.zhuss.db.entity.OpLog;
import com.zhuss.enums.LogActionEnum;
import com.zhuss.enums.MoudleEnum;
import com.zhuss.exception.DzcxException;
import com.zhuss.exception.ExceptionCode;
import com.zhuss.product.db.dao.ext.LineDao;
import com.zhuss.product.db.dao.ext.LineSiteDao;
import com.zhuss.product.db.entity.Line;
import com.zhuss.product.db.entity.LineSite;
import com.zhuss.product.processor.cache.LineCache;
import com.zhuss.product.processor.cache.LineSiteCache;
import com.zhuss.product.processor.cache.SiteCache;
import com.zhuss.util.JsonUtil;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;

import java.util.Date;

/**
 * @author summer
 * @date 2020/12/31 9:04
 */
@Component("lineBind")
public class LineBind extends AbstractProcessor<Object, BindLineBo> implements Processor<Object, BindLineBo> {


    @Autowired
    private LineSiteDao lineSiteDao;

    @Autowired
    @Qualifier("lineCache")
    private LineCache lineCache;
    
    @Autowired
    @Qualifier("lineSiteCache")
    private LineSiteCache lineSiteCache;

    @Autowired
    private LineDao lineDao;

    @Autowired
    @Qualifier("siteCache")
    private SiteCache siteCache;

    @Override
    public void check(BindLineBo bo) throws DzcxException {
        if (bo.getLineId() == null) {
            throw new DzcxException(ExceptionCode.线路ID为空);
        }

        if (bo.getSiteList().size() >0 && bo.getSiteList() !=null) {
            DetailLineVo lineVo = lineCache.get(bo.getLineId());
            String start = siteCache.get(bo.getSiteList().get(0).getSiteId()).getName();
            if (!lineVo.getStartPointName().equals(start)) {
                throw new DzcxException(ExceptionCode.首站名称和线路起点名称不一致);
            }
            if (bo.getSiteList().size() >1) {
                String end = siteCache.get(bo.getSiteList().get(bo.getSiteList().size() -1).
                        getSiteId()).getName();
                if (!lineVo.getEndPointName().equals(end)) {
                    throw new DzcxException(ExceptionCode.末站名称和线路终点名称不一致);
                }
            }
        }

    }

    @Override
    public Object process(BindLineBo bo) throws DzcxException {

        //根据线路id删除已绑定的站点。
        LineSite lineSite = new LineSite();
        lineSite.setLineId(bo.getLineId());
        lineSite.setDeleted(bo.getDeleted());
        lineSite.setModifierId(bo.getConcurrentUserId());
        lineSite.setModifier(bo.getConcurrentUserName());
        lineSite.setModifyTime(new Date());
        lineSiteDao.deleteByLineId(lineSite);
        //插入t_line_site表
        Integer time = 0;  //线路的间隔时间
        for (CreateLineSiteBo siteBo :bo.getSiteList()) {
            if (siteBo.getPrice() == null ) {
                throw new DzcxException(ExceptionCode.票价权重为空);
            }
            if (siteBo.getNearestDuration() == null ) {
                throw new DzcxException(ExceptionCode.间隔时间为空);
            }
            lineSiteSave(siteBo, bo.getLineId());
            time = time + (siteBo.getNearestDuration() == null ? 0 : siteBo.getNearestDuration());
        }
        //更新线路表line
        Line line = lineDao.selectByPrimaryKey(bo.getLineId());
        line.setIntervals(time);
        lineDao.updateByPrimaryKeySelective(line);
        lineCache.clear(line.getLineId());
        lineSiteCache.clear(line.getLineId());
        return null;
    }

    public void lineSiteSave(CreateLineSiteBo param, Long lineId) {
        LineSite record = new LineSite();
        BeanUtils.copyProperties(param, record);
        record.setLineId(lineId);
        record.setCreatorId(param.getConcurrentUserId());
        record.setCreator(param.getConcurrentUserName());
        record.setCreateTime(new Date());
        lineSiteDao.create(record);
    }

    @Override
    public Object log(BindLineBo param) throws DzcxException {
        OpLog record = new OpLog();
    	record.setMoudle(MoudleEnum.商品.key());
        record.setAction(LogActionEnum.线路站点创建.key());
        record.setTargetId(param.getLineId());
        record.setTargetName(param.getName());
        record.setContent("参数: " + JsonUtil.toJson(param));
        record.setReason(param.getExceptionReason());

        record.setCreatorId(param.getConcurrentUserId());
        record.setCreator(param.getConcurrentUserName());
        record.setCreateTime(new Date());
        log(record);
        return null;
    }
}
